package cn.lf.android.lawfirm.worklog;


import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.changeapproval.controller.ChangeUnderListConroller;
import cn.lf.android.lawfirm.changeapproval.view.ChangeUndertakeAdapter;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.undertake.approval.CaseDetailActivity;
import cn.lf.android.lawfirm.worklog.controller.WorkLogListConroller;
import cn.lf.android.lawfirm.worklog.molde.WorkLog;
import cn.lf.android.lawfirm.worklog.view.WorkLogAdapter;

import static cn.lf.android.lawfirm.R.id.img_left;

public class WorkLogSearchListActivity extends OrdinaryBaseActivity
        implements WorkLogListConroller.WorkLogListCallback {

    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private WorkLogAdapter mWorkLogAdapter;
    private List<WorkLog> list = new ArrayList<WorkLog>();

    private WorkLogListConroller mWorkLogConroller;
    private int pageIndex = 1;
    /**
     * 分别为：all（全部）、checked（已查阅）、waitCheck（待查阅）、backed（已退回）（必填）
     **/
    private String state = "waitCheck";
    private String flag = "";//日志查阅 传1  工作日志传0
    @Override
    public int getChildView() {
        return R.layout.activity_undertake_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        flag=getIntent().getStringExtra("flag");
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mWorkLogAdapter = new WorkLogAdapter(this, list, 0);
        mListView.setAdapter(mWorkLogAdapter);
        mRL_search.setClickable(false);
        mSearch.requestFocus();
        openKeybord();
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case img_left:
                hintKbTwo();
                finish();
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("搜索");
        mRL_search.setVisibility(View.VISIBLE);
        mSearch.setHint("请输入日志主题");
    }

    private void setLisenters() {
        mListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hintKbTwo();
                return false;
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                if (mSearch.getText().toString().length() > 0) {
                    pageIndex = 1;
                    list.clear();
                    mWorkLogConroller.onWorkLogList(flag, mSearch.getText().toString().trim(), String.valueOf(pageIndex), state);
                }
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                pageIndex++;
                mWorkLogConroller.onWorkLogList(flag, mSearch.getText().toString().trim(), String.valueOf(pageIndex), state);
            }
        });
//        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(WorkLogSearchListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);
//                startActivity(intent);
//            }
//        });
        mSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (null != event) {
                    pageIndex = 1;
                    list.clear();
                    mWorkLogConroller.onWorkLogList(flag, mSearch.getText().toString().trim(), String.valueOf(pageIndex), state);
                }
                return false;
            }
        });
        mSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //这里是联通的，有变化就进行网络请求
                if (mSearch.getText().toString().trim().length() == 0) {
                    mClean.setVisibility(View.GONE);
                    list.clear();
                    mWorkLogAdapter.notifyDataSetChanged();
                    return;
                } else {
                    mClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {
        mWorkLogConroller = new WorkLogListConroller(this, this);
    }

    @Override
    public void onWorkLogListSuccessed(List<WorkLog> beans) {
        list.addAll(beans);
        mWorkLogAdapter.notifyDataSetChanged();
        if (list.size() == 0) {
            noData2.setVisibility(View.VISIBLE);
        } else {
            noData2.setVisibility(View.GONE);
        }
    }

    @Override
    public void onWorkLogListFailed() {

    }
}
